<?php
/**
 * 权限认证分配
 * 
 * @author  Lee <a605333742@gmail.com>
 * @since   2015-6-15
 */
namespace Admin\Controller;
//引用分页类
use Think\Page;

class CommonController extends EmptyController{
    /*
     *构造函数
     * 
     * @return # 
     */
    public function _initialize(){
        //第一步：判断是否开启了权限认证；判断当前控制器是否不需要认证
        if(C("AUTH_ON") && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))){
            if(!I("post.authId")){
                if(!isset($_SESSION[C('USER_AUTH_KEY')])){
                    $this->redirect("Public/login");
                }
                //查询操作是否在所属组中，不在则没有权限
                if(CONTROLLER_NAME!='Index'){
                    $nodes  =A("Public")->ckeckAuth();
                    $data   =array();
                    foreach($nodes as $vo){
                        $data[] =$vo["controller"];
                    }
                    if(!in_array(CONTROLLER_NAME,$data)){
                        $this->error("无权进行此操作");
                    }
                }
            }
        }
    }

    /*
     * 根据表单生成查询条件-进行列表过滤
     *
     * @param string $name 数据对象名称
     *
     * @return array
     */
    public function _search($name=''){
        //生成查询条件
        if (empty ($name)) {
            $name = CONTROLLER_NAME;
        }
        $model      =D( $name );        
        $map        =array ();
        $fieldArray =array();
        if ($model->getDbFields()){
            $fieldArray = $model->getDbFields();
        }
        if ($model->viewFields){
            foreach ( $model->viewFields as $key => $val ){
                $fieldArray =array_merge($fieldArray, $model->viewFields[$key]);
            }
            unset($fieldArray['_on']);
        }
        //show_bug($_REQUEST [$val]);exit;
        foreach ( $fieldArray as $key => $val ) {				
            if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != ''){
                //特别指定一些字段进行模糊查询
                $likeArray = array(
                    'name',
                    'en_name',
                    'title',
                    'username',
                    'nickname',
                    'remark',
                    'content',
                    'question',
                    'theme',
                    'desc_',
                    'en_desc_',
                    'subjects',
                    'controller',
                    'action',
                    'result',
                    'answer',
                    'add_time',
                );
                if (in_array($val, $likeArray)){
                    //模糊查询
                    $map [$val] = array('like', '%'.trim($_REQUEST [$val]).'%');
                } else {
                    //精确查询
                    $map [$val] = $_REQUEST [$val];
                }
            }
        }
        return $map;
    }
    
    /*
     * 根据表单生成查询条件-进行列表过滤
     *
     * @param object 	$model 		数据对象
     * @param array 	$map 		过滤条
     * @param string 	$sortBy 	排序
     * @param boolean 	$asc 		是否正序
     * @param string 	$countPk 	主键
     * @param string 	$field 		提取字段
     *
     * @return array    #
     */
    public function _list($model, $map, $sortBy='',$asc =TRUE, $countPk="id", $field="*") {
        //验证传参的有效性
        if (empty($asc)){
                $asc = false;
        }
        if (isset($model->pk)) {
                $countPk = $model->pk;
        }
        if (empty($countPk)){
                $countPk = "id";
        }
        //排序字段 默认为主键名
        $orderSign  =TRUE;
        if (!empty ( $_REQUEST ['_order'] )) {
            $order  =$_REQUEST ['_order'];
        } else {
            if(!empty($sortBy)){
                $order  =$sortBy;
                if(is_array($sortBy)){
                    $orderSign  =FALSE;
                }
            }else{
                $order  =$countPk;
            }
        }
        //排序方式默认按照倒序排列
        //接受 sort参数 0 表示倒序 非0都 表示正序
        if($orderSign){
            if (!empty ( $_REQUEST ['_sort'] )) {
                $sort = $_REQUEST ['_sort'] == 'asc' ? 'asc' : 'desc'; //zhanghuihua@msn.com
            }else{
                $sort = $asc? 'asc' : 'desc';
            }
        }
        //取得满足条件的记录数
        $count = $model->where ( $map )->count ();
        if ($count > 0) {
            //创建分页对象            
            if($_REQUEST['numPerPage']){
                $_REQUEST ['listRows']=I("request.numPerPage");
            }
            if (! empty ( $_REQUEST ['listRows'] )) {
                    $listRows   =$_REQUEST ['listRows'];
            } else {
                    $listRows   =20;
            }
            $p = new Page ( $count, $listRows );
            //分页查询数据
            if($orderSign){
                $voList = $model->where($map)->group($countPk)->field($field)->order("`" .$order ."` ". $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( ); 
            }else{
                $voList = $model->where($map)->group($countPk)->field($field)->order($order)->limit($p->firstRow . ',' . $p->listRows)->select ( ); 
            }
            //return $model->getLastSql();
            //show_bug($model->getLastSql());
            //show_bug($voList);exit;
            //exit;
            //分页跳转的时候保证查询条件
            foreach ( $map as $key => $val ) {
                if (! is_array ( $val )) {
                    $p->parameter .= "$key=" . urlencode ( $val ) . "&";
                }
            }
            //分页显示
            $page = $p->show ();
            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式
            //模板赋值显示
            $this->assign ( 'list', $voList );
            $this->assign ( 'sort', $sort );
            $this->assign ( 'order', $order );
            $this->assign ( 'sortImg', $sortImg );
            $this->assign ( 'sortType', $sortAlt );
            $this->assign ( "page", $page );
        }
        //zhanghuihua@msn.com
        $this->assign ( 'totalCount', $count );
        $this->assign ( 'numPerPage', $p->listRows );
        $this->assign ( 'currentPage', !empty($_REQUEST[C('VAR_PAGE')])?$_REQUEST[C('VAR_PAGE')]:1);
        
    }  
    
    /*
     * 显示首页也
     * 
     * @return #
     */
    public function index($db='',$sort='id',$sortBy=FALSE){
        $map = $this->_search($db);
        if (method_exists($this, '_filter')) {
            $this->_filter($map);
        }
        $map['status']  =array("neq",0);
        if($db==""){
            $model      =D(CONTROLLER_NAME);
        }else{
            $model      =D($db);  
        }
        if (!empty ($model)) {
            $this->_list($model, $map,$sort,$sortBy);
        }
        $this->display();
        return;
    }
    
    /*
     * 显示新增编辑页面
     * 
     * @return #
     */
    public function show(){
        if(I("request.sign")){
            $this->assign("_sign",I("request.sign",'','code'));
        }
        if(I("request.id")){
            if(!I("request.db")){
                $model  =D(CONTROLLER_NAME);
            }else{
                $model  =D(I("request.db",'','code'));  
            }
            $id = $_REQUEST [$model->getPk()];
            if(!is_numeric($id)){
                $id=code($id);
            }
            $vo = $model->getById($id);
            $this->assign('vo', $vo);
        }
        $this->display();
    }    
    
    /*
     * 编辑数据
     * 
     * @return # 
     */
    public function edit(){
        if(I("request.id")){
            if($this->update()){
                $this->success("修改成功");
            }else{
                if(!empty($this->update()['name'])){
                    $this->error($this->update()['name']);
                }else{
                    $this->error("修改失败"); 
                }   
            }
        }else{
            if($this->insert()=="true"){
                $this->success("新增成功");
            }else{
                if(!empty($this->insert()['name'])){
                    $this->error($this->insert()['name']);
                }else{
                    $this->error("新增失败");
                }
            }
        }
    }    
    
    /*
     * 新增数据
     * 
     * @return #
     */
    function insert(){
        if(!I("request.db")){
            $model  =D(CONTROLLER_NAME);
        }else{ 
            $model  =D(I("request.db"));
        }  
        $field  =$model->getDbFields();
        if(in_array("top",$field)){
            $top            =$model->max("top");
            $_POST['top']   =$top+1;
        }
        
        if (false === $model->create()) {
            return $model->getError();
        }
        //保存当前数据对象
        $list = $model->add();
        if ($list !== false) {
            if($this->addLog("新增",I("request.name"))){
                return TRUE;
            }else{
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
    
    /*
     * 更新数据
     * 
     * $return #
     */
    function update(){
        if(!I("request.db")){
            $model  =D(CONTROLLER_NAME);
        }else{
            $model  =D(I("request.db"));  
        }
        //判断是否有图片存在，标签图片是否更改
        $imageIcon  =$model->getById(I("request.id"));
        if($imageIcon['icon'] && $imageIcon['icon']!=I("request.icon")){
            session('oldImg',$imageIcon['icon']);
        }else if($imageIcon['image'] && $imageIcon['image']!=I("request.image")){
            if($imageIcon['small_image']){
                session('oldSmallImg',$imageIcon['small_image']);
            }
            session('oldImg',$imageIcon['image']);
        }else if($imageIcon['logo'] && $imageIcon['logo']!=I("request.logo")){
            session('oldImg',$imageIcon['logo']);
        }
        if(I("request.top") && I("request.top")>0){
            $top    =$model->max("top");
            $_POST['top']   =$top+1;
        }else{
            unset($_POST['top']);
        }
        if (false === $model->create()) {
            $this->error($model->getError());
        }        
        // 更新数据
        $list = $model->save();
        if (false !== $list) {
            if($this->addLog("修改",I("request.name"))){
                unlink('Application/Public/Upload/'.session("oldImg"));
                if(session("oldSmallImg")){
                    unlink('Application/Public/Upload/'.session("oldSmallImg"));
                }
                return TRUE;
            }else{
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
    
    /*
     * 新增日志信息
     * 
     * @return #
     */
    public function addLog($g='',$name=''){
        if(!I("request.db")){
            $db =CONTROLLER_NAME;
        }else{
            $db =I("request.db");
        }
        if(is_array($name)){
            $name   =  implode(",",$name);
        }
        $data       =array(
            'user_id'   =>session("authId"),
            'db'        =>$db,
            'content'   =>$g." 了 ".$name ." 数据"
        );
        if(M("Log")->add($data)){
            return TRUE;
        }else{
            return FALSE;
        }
    }  
    
    /*
     * 删除数据
     * 
     * @return #
     */
    public function del(){
        //删除指定记录
        if(!I("request.db")){
            $model  =D(CONTROLLER_NAME);
        }else{
            $model  =D(I("request.db","","code"));  
        }
        if (!empty ($model)) {
            $pk = $model->getPk();
            $id =$_REQUEST [$pk];
            if(!is_array($id)){                
                $id = explode(",",code($id));
            }           
            if (isset ($id)) {
                $condition = array($pk => array('in', implode(',', $id)));
                $list = $model->where($condition)->setField('status',0);
                if ($list !== false && $this->addLog("删除",$id)) {
                    $this->success('删除成功！');
                } else {
                    $this->error('删除失败！');
                }
            }else{
                $this->error('非法操作');
            }
        }
    }
    
    /*
     * 彻底删除记录（一般情况下不要使用）
     * 
     * @return bool
     */
    public function delete(){
        //删除指定记录
        if(!I("request.db")){
            $model  =D(CONTROLLER_NAME);
        }else{
            $model  =D(I("request.db","","code"));  
        }
        if (!empty ($model)) {
            $pk = $model->getPk();
            $id = $_REQUEST [$pk];
            if(!is_array($id)){                
                $id = explode(",",code($id));
            }
            if (isset ($id)) {
                $condition = array($pk => array('in', implode(',', $id)));
                if (false !== $model->where($condition)->delete()) {
                    $this->success('删除成功！');
                } else {
                    $this->error('删除失败！');
                }
            }else{
                $this->error('非法操作');
            }
        }
    }
    
    /*
     * 处理提交时间范围查询
     *
     * @param  string $startTimeString 开始时间表单名
     * @param  string $endTimeString   结束时间表彰名
     * 
     * @return array                   数据库查询条件
     */
    public function processTimeMap( $startTimeString, $endTimeString ){
        //没有设置就默认1星期前
        $startTime  =!empty( $_REQUEST[$startTimeString] ) ? strtotime( $_REQUEST[$startTimeString] ) : time() - 604800 ; 
        //没有设置就默认当前时间
        $endTime    =!empty( $_REQUEST[$endTimeString] ) ? strtotime( $_REQUEST[$endTimeString] ) : time();
        if( $endTime < $startTime ){
            $this->error( '结束时间不能早于开始时间!' );
        }
        return array( 'between', array( $startTime, $endTime ) );
    }
    
    /*
     * 图片上传
     * 
     * @return #
     */
    public function upImg(){
        if(!empty($_FILES['images']['name'])){
            if(I("request.ajax")==1){
                $rootPath   ='title/';
            }else if(I("request.ajax")==2){
                $rootPath   ='top/';
            }else if(I("request.ajax")==3){
                $rootPath   ='section/';
            }else if(I("request.ajax")==4){
                $rootPath   ='user/';
            }else if(I("request.ajax")==5){
                $rootPath   ='club/';
            }else if(I("request.ajax")==6){
                $rootPath   ='data/';
            }else if(I("request.ajax")==7){
                $rootPath   ='mod/';
            }else if(I("request.ajax")==8){
                $rootPath   ="club_img/";
            }else if(I("request.ajax")==9){
                $rootPath   ="answer/";
            }else if(I("request.ajax")==10){
                $rootPath   ="draw/";
            }else{
                $rootPath   ='common/';
            }
            $config =array(
                'rootPath'  =>'./Application/Public/Upload/',       //根目录
                'savePath'  =>$rootPath,                            //保存路径
                'maxSize'   =>'0',                                  //上传的文件大小限制 (0-不做限制)
                'exts'      =>array('ico','jpg','png','gif','jpeg','doc','docx','xls','xlsx','pdf','txt','ppt','pptx') //允许上传的文件后缀
            );
            //附件被上传到路径：根目录/保存目录路径/创建日期目录
            $new_upload =new \Think\Upload($config);
            //uploadOne会返回已经上传的附件信息
            $upload     =$new_upload->uploadOne($_FILES['images']);
            if(!$upload){
                echo $new_upload->getError();
            }else{  
                if(I("request.ajax")==8){
                    $map['club_id'] =I("request.club_id");
                    $map['image']   =$upload['savepath'].$upload['savename'];
                    D("ClubImg")->add($map);
                }else{
                    echo ($upload['savepath'].$upload['savename']);
                }                                              
            }           
        }
    }
    
    /*
     * 更改状态
     * 
     * @return #
     */
    public function changeStatus(){ 
        if(!I("request.db")){
            $model  =D(CONTROLLER_NAME);
        }else{
            $model  =D(I("request.db","","code"));  
        }
        if (!empty ($model)) {
            $pk = $model->getPk();
            $id =$_REQUEST [$pk];
            if(!is_array($id)){                
                $id = explode(",",code($id));
            }           
            if (isset ($id)) {
                $condition = array($pk => array('in', implode(',', $id)));
                $list = $model->where($condition)->setField('status',I("request.status","","code"));
                if ($list !== false && $this->addLog("修改",I("request.id","","code"))) {
                    $this->success('状态修改成功！');
                } else {
                    $this->error('状态修改失败！');
                }
            } else {
                $this->error('非法操作');
            }
        }
    }
    
    /*
     * 将模糊的字段名转换为id字符串列
     * 
     * @return str  $ids
     */
    public function getIds($field='',$name='name',$db='',$val='id'){        
        $map[$name]     =array('like',"%".trim($field)."%");
        $map['status']  =array('neq',0);
        $db             =D($db);
        $arr            =$db->where($map)->select();
        $arrIds         =array();
        foreach($arr as $vo){
            $arrIds[]   =$vo[$val];             
        }            
        $ids=array("in",implode(',', $arrIds));
        return $ids;
    } 
}